package com.newworld.dmsapp2.activity;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.newworld.dmsapp2.BaseActivity;
import com.newworld.dmsapp2.R;
import com.newworld.dmsapp2.bean.User;
import com.newworld.dmsapp2.ui.view.CustomDialog;
import com.newworld.dmsapp2.ui.view.CustomLoadingDialog;
import com.newworld.dmsapp2.util.Config;
import com.newworld.dmsapp2.util.HttpUtil;
import com.newworld.dmsapp2.util.SingleSample;
import com.newworld.dmsapp2.util.Tool;

public class ServiceScheduleUserValueActivity extends BaseActivity implements
		OnClickListener {
	private RatingBar RB_genral, RB_quality, RB_speed, RB_attitude;
	private EditText value_describe;
	private Button accomplish_value;
	private TextView titleCenterTv;
	private LinearLayout leftLinLayout;
	private User user;
	private Intent intent;
	private Bundle bundle;
	private String functioncode;
	private String repairId;
	private String sessionId;
	private CustomLoadingDialog uploadingdialog;

	Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case Config.HANDLER_MSG_VALUEPOPST_SUCCESS:
				if (uploadingdialog.isShowing()) {
					uploadingdialog.dismiss();
				} else {
					uploadingdialog.show();
				}
			
				finish();
				showShortToast((String) msg.obj);
				break;
			case Config.HANDLER_MSG_FAIL:
				if (uploadingdialog != null) {
					if (uploadingdialog.isShowing()) {
						uploadingdialog.dismiss();
					} else {
						uploadingdialog.show();
					}
				}
				showShortToast((String) msg.obj);
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
	};
	private String areaType;

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_serviceschedule_userassessment);
		initData();
		initViews();
		initEvents();
	}
	
	private void initData() {
		intent = getIntent();
		bundle = intent.getExtras();
		functioncode = bundle.getString(Config.FUNCTIONCODE);
		areaType = bundle.getString(Config.AREATYPE);
		repairId = bundle.getString(Config.REPAIRID);
		user = SingleSample.getInstance(this).getUser();
		if (null != user) {
			if (null != user.getSessionId()
					&& !user.getSessionId().equals("")) {
				sessionId = user.getSessionId();
			}
		}
	}
	
	@Override
	protected void initViews() {
		// TODO Auto-generated method stub
		titleCenterTv = (TextView) findViewById(R.id.titlebar_left_center_title);
		leftLinLayout = (LinearLayout) findViewById(R.id.titlebar_left_left_layout);
		RB_genral = (RatingBar) findViewById(R.id.sch_generalassessment_ratingbar);
		RB_quality = (RatingBar) findViewById(R.id.sch_accomplishquality_ratingbar);
		RB_speed = (RatingBar) findViewById(R.id.sch_servicespeed_ratingbar);
		RB_attitude = (RatingBar) findViewById(R.id.sch_serviceattitude_ratingbar);
		value_describe = (EditText) findViewById(R.id.assessmentdescribe);
		accomplish_value = (Button) findViewById(R.id.accomplishassessment);

	}

	@Override
	protected void initEvents() {
		// TODO Auto-generated method stub
		titleCenterTv.setText("业主评价");
		leftLinLayout.setOnClickListener(this);
		accomplish_value.setOnClickListener(this);

	}

	

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.titlebar_left_left_layout:
			// TODO Auto-generated method stub
		
			finish();
			break;
		case R.id.accomplishassessment:
			// TODO Auto-generated method stub
			final CustomDialog dialog = new CustomDialog(
					ServiceScheduleUserValueActivity.this);
			dialog.setTitle("完成评价");
			dialog.setDetial("是否确定完成评价？");
			dialog.setLeftOnClick(new OnClickListener() {
				@Override
				public void onClick(View v) {

					dialog.dismiss();
				}
			});
			dialog.show();
			dialog.setRightOnClick(new OnClickListener() {
				@Override
				public void onClick(View v) {
					dialog.dismiss();
					uploadingdialog = new CustomLoadingDialog(
							ServiceScheduleUserValueActivity.this, "正在上传...");
					if (uploadingdialog.isShowing()) {
						uploadingdialog.dismiss();
					} else {
						uploadingdialog.show();
					}
					new Thread(postEvaluateCompeteRun).start();
				}
			});
			dialog.show();
			break;

		default:
			break;
		}
	}

	Runnable postEvaluateCompeteRun = new Runnable() {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			postEvaluateCompete();
		}
	};

	// 完成评价
	private void postEvaluateCompete() {
		if (Tool.isNetworkAvailable(ServiceScheduleUserValueActivity.this)) {
			try {
				String result = null;
				Map<String, String> map = new HashMap<String, String>();
				String url = "";
				if (functioncode
						.equals(Config.FUNCTION_REAL_GETREPAIRPROGRESSLISTOFOWNER)
						|| functioncode
								.equals(Config.FUNCTION_REAL_GETREPAIRPROGRESSLISTOFNONOWNER)
						|| functioncode
								.equals(Config.FUNCTION_REAL_GETOUTDOORPROGRESSLISTOFOWNER)) {
					url = Config.SERVER_URL
							+ user.getFunctionlistFromUser(
									Config.FUNCTION_REAL_EVALUATE).get(0)
									.getFunctionUrl();
					map.put("functionCode", Config.FUNCTION_REAL_EVALUATE);
				} else {
					url = Config.SERVER_URL
							+ user.getFunctionlistFromUser(
									Config.FUNCTION_REAL_EVALUATE_OUTDOOR)
									.get(0).getFunctionUrl();
					map.put("functionCode",
							Config.FUNCTION_REAL_EVALUATE_OUTDOOR);
				}

				map.put("repairId", repairId);
				map.put("averageStar", (int) RB_genral.getRating() + "");
				map.put("qualityStar", (int) RB_quality.getRating() + "");
				map.put("speedStar", (int) RB_speed.getRating() + "");
				map.put("attitudeStar", (int) RB_attitude.getRating() + "");
				map.put("evaluateDescript", value_describe.getText().toString()
						+ "");
				
				result = HttpUtil.postRequestSe(url, map, sessionId);
				Log.e("完成评价的result", result);
				String message = new JSONObject(result).getString("message");
				String flag = new JSONObject(result).getString("flag");
				if (flag.equals(Config.CONNECT_SUCCESS2)) {
					mHandler.obtainMessage(
							Config.HANDLER_MSG_VALUEPOPST_SUCCESS, message)
							.sendToTarget();// 通知主线程数据发送成功

				} else {
					mHandler.obtainMessage(Config.HANDLER_MSG_FAIL, message)
							.sendToTarget();
				}

			} catch (Exception e) {
				Log.e("ServiceScheduleActivity", "json数据获取报错");
				mHandler.obtainMessage(Config.HANDLER_MSG_FAIL, "json数据获取报错")
						.sendToTarget();
				e.printStackTrace();
			}

		} else {
			mHandler.obtainMessage(Config.HANDLER_MSG_FAIL, "请设置网络")
					.sendToTarget();
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) { // 监控/拦截/屏蔽返回键
			
			finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
